/*
 *  Copyright 2009 Matthieu Chase Heimer
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.

 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
package com.macro10.xroads.commands;

import com.macro10.switchboard.Request;
import com.macro10.switchboard.Session;
import com.macro10.switchboard.service.UserTransaction;
import com.macro10.xroads.entity.Area;
import com.macro10.xroads.entity.Avatar;
import com.macro10.xroads.entity.Exit;
import com.macro10.xroads.service.AreaService;
import com.macro10.xroads.service.AvatarService;

/**
 * Requires headers: exit
 * Filters: in_world
 * 0=north,1=east,2=south,3=west,4=up,5=down
 */
public class Move extends AbstractMoveCommand {

    @Override
    public ExecuteResult execute(Request request, Session session) {
        String avatarName = (String) session.getAttribute("avatar");
        String exitId = request.getHeader("exit");

        int exitNum = 0;
        try {
            exitNum = Integer.parseInt(exitId);
        } catch (Exception e) {
            session.writeError(request.getURL() + ":exit.not.valid");
            return ExecuteResult.START_POST;
        }

        UserTransaction ut = null;
        try {
            ut = new UserTransaction();
            AvatarService avatarService = new AvatarService(ut);
            Avatar avatar = avatarService.getAvatar(avatarName);

            Exit matchingExit = null;
            for (Exit exit : avatar.getArea().getExits()) {
                if (exit.getPosition() == exitNum) {
                    matchingExit = exit;
                    break;
                }
            }

            AreaService areaService = new AreaService(ut);
            if (matchingExit == null) {
                session.writeError(request.getURL() + ":exit.not.found");
                ut.commit();
            } else if (!areaService.doesAreaExist(matchingExit.getTargetName())) {
                session.writeError(request.getURL() + ":exit.invalid");
                ut.commit();
            } else {
                Area area = areaService.getArea(matchingExit.getTargetName());
                avatar.setArea(area);
                String areaName = area.getAreaName();
                session.writeText(request.getURL() + ":moved");
                showArea(session, area, avatar);
                ut.commit();
                session.setAttribute("area", areaName);

//avatars:chase|m|earth|novice|weapon|armor,christine|f|earth|novice|weapon|armor
//monsters:123456|earth|wolf
//items:1234567|neutral|dagger
//
//Mages:Novice,Conjuror,Sorceror,Mage,Enchanter,Wizard
//Fighters:Novice,Barbarian,Soldier,Swordsman,Gladiator,Knight,Lord
//Mage-Fighters:Novice,Journeyman,Bard,Gypsy,Sage,Mediator
//Fighter-Mages:Novice,Mercenary,Rogue,Ranger,Champion,Destroyer
            }
        } catch (Exception e) {
            session.writeException(request.getURL() + ":move.failed", e);
            if (ut != null) {
                ut.rollback();
            }
        }
        return ExecuteResult.START_POST;
    }
}
